


$.ajax({
    type: 'get',
    url: '/categories',
    success: function (res) {
        var html = template('listTpl', { data: res });
        $('#category').html(html);
    }
});

$('#feature').on('change', function () {
    var formdata = new FormData();
    formdata.append('cover', this.files[0]);
    $.ajax({
        type: 'post',
        url: '/upload',
        data: formdata,
        processData: false,
        contentType: false,
        success: function (res) {

            $('#thumbnail').val(res[0].cover)
        }
    })
});

$('#article').on('submit', function () {
    var formdata = $(this).serialize();
    $.ajax({
        type: 'post',
        url: '/posts',
        data: formdata,
        success: function () {
            location.href = '/admin/posts.html'
        },
        error(err) {
            console.log(err);
        }
    });
    return false;
});

//从浏览器的地址栏中获取查询参数
function getmessage(name) {
    var queryArr = location.search.substr(1).split('&');
    for (var i = 0; i < queryArr.length; i++) {
        var nameArr = queryArr[0].split("=")
        if (nameArr[0] == name) {
            return nameArr[1];
        }
    }
    return -1;

}

id = getmessage('id');
if (id != -1) {

    $.ajax({
        type: 'get',
        url: '/posts/' + id,
        success: function (response) {
            $.ajax({
                type: 'get',
                url: '/categories',
                success: function (res) {
                    response.categories = res;
                    var html = template('articleTpl', { data: response });

                    $('#biaodan').html(html);
                }
            });

        }


    })
}

$('#biaodan').on('submit', '#articleEdit', function () {
    var id = $(this).attr('data-id');
    var formData = $(this).serialize();
    $.ajax({
        type: 'put',
        url: '/posts/' + id,
        data: formData,
        success: function (res) {
            console.log(res);
            location.href = '/admin/posts.html'
        }
    })

    return false;
})
